package com.yue92.datalife.exception;

import com.yue92.datalife.common.ICode;
import com.yue92.datalife.response.ResponseResource;
import org.springframework.util.StringUtils;

public class UncheckedException extends RuntimeException {

    private ICode code;

    private String message;

    private Object data;

    public UncheckedException(ICode code) {
        this.code = code;
    }

    public UncheckedException(ICode code, String message) {
        this.code = code;
        this.message = message;
    }

    public UncheckedException(ICode code, Object data) {
        this.code = code;
        this.data = data;
    }

    public ResponseResource<?> getResult() {
        String message;
        if (StringUtils.isEmpty(this.message)) {
            message = code.getMessage();
        } else {
            message = this.message;
        }

        return ResponseResource.failure(code, message, data);
    }

}
